package com.memory.admin.sell.controller;

import com.memory.admin.system.validator.UserValid;
import com.memory.common.utils.ResultVoUtil;
import com.memory.common.vo.ResultVo;
import com.memory.component.actionLog.action.UserAction;
import com.memory.component.actionLog.annotation.EntityParam;
import com.memory.modules.business.domain.sell.Sell;
import com.memory.modules.business.domain.user.Account;
import com.memory.modules.business.service.earn.AccountEarnRecordService;
import com.memory.modules.business.service.sell.SellService;
import com.memory.modules.business.service.user.AccountService;
import com.memory.modules.system.domain.ActionLog;
import com.memory.modules.system.domain.User;
import lombok.RequiredArgsConstructor;
import org.apache.commons.compress.utils.Lists;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.WebRequest;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * @PackageName: com.memory.admin.sell.controller
 * @ClassName: SellController
 * @Description:
 * @author: zhuguangxun
 * @date 2021/3/10
 */
@Controller
@RequestMapping("/business/sell/")
@RequiredArgsConstructor
public class SellController {

    @InitBinder
    public void initBinder(WebDataBinder binder, WebRequest request) {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }

    private final SellService sellService;

    private final AccountService accountService;

    @GetMapping("list")
    public String list(Model model, Sell sell) {
        ExampleMatcher matcher = ExampleMatcher.matching();
        Example<Sell> example = Example.of(sell, matcher);
        final Page<Sell> list = sellService.findList(example);
        model.addAttribute("list", list.getContent());
        model.addAttribute("page", list);
        return "/business/sell/list";
    }


    /**
     * 跳转到添加页面
     */
    @GetMapping("/add")
    public String toAdd(Model model) {
        findUserList(model);
        return "/business/sell/add";
    }

    /**
     * 跳转到编辑页面
     */
    @GetMapping("/edit/{id}")
    public String toEdit(@PathVariable("id") Long id, Model model) {
        final Optional<Sell> byId = sellService.findById(id);
        model.addAttribute("sell", byId.orElse(new Sell()));
        findUserList(model);
        return "/business/sell/add";
    }

    /**
     * 保存添加/修改的数据
     *
     * @param valid 验证对象
     * @param user  实体对象
     */
    @PostMapping("/save")
    @ResponseBody
    @com.memory.component.actionLog.annotation.ActionLog(key = UserAction.USER_SAVE, action = UserAction.class)
    public ResultVo save(@EntityParam Sell sell) {
        if (sell.getId() != null) {
            sellService.update(sell.getId(), sell);
        } else {
            final Sell sell1 = sellService.create(sell);
        }
        return ResultVoUtil.SAVE_SUCCESS;
    }

    private void findUserList(Model model){
        Account account = new Account();
        Example<Account> accountExample = Example.of(account);
        final List<Account> list1 = accountService.findList(accountExample);
        model.addAttribute("accountList",list1);
    }

}
